package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.BBSConAction;
import leeon.bbsbrower.actions.Config;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class SendDocView extends ViewPart {
	public static final String ID = "bbsbrower.view.senddoc";
	
	private Text board = null;
	private Text title = null;
	private Text doc = null;	

	//0 文章列表, 1 回文章
	private static int en = 0;
	
	
	//从哪一篇文章进入
	private static int d = 0;
	
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		//布局
		parent.setLayout(null);
				
		board = new Text(parent, SWT.BORDER|SWT.READ_ONLY);		
		board.setBounds(90, 10, 200, 30);		
		board.setFont(ApplicationWorkbenchWindowAdvisor.listFont);		
		board.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车到title
				if (e.keyCode == 13) {
					title.setFocus();
					e.doit = false;	
				//esc 退出
				} else if (e.keyCode == SWT.ESC) {
					actionToDoc();					
					e.doit = false;					
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});		
		final Label idLabel = new Label(parent, SWT.NONE);
		idLabel.setText("版面:");
		idLabel.setBounds(10, 10, 70, 30);
		idLabel.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		title = new Text(parent, SWT.BORDER);
		title.setBounds(90, 50, 900, 30);
		title.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		title.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车到doc
				if (e.keyCode == 13) {
					doc.setFocus();
					e.doit = false;
				//退出
				} else if (e.keyCode == SWT.ESC) {
					actionToDoc();					
					e.doit = false;					
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 119) {
					actionSend();
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
		final Label msgLabel = new Label(parent, SWT.NONE);
		msgLabel.setText("标题:");
		msgLabel.setBounds(10, 50, 70, 30);
		msgLabel.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		

		doc = new Text(parent, SWT.BORDER|SWT.V_SCROLL);		
		doc.setBounds(90, 90, 900, 550);	
		doc.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		doc.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//ctrl + w发送
				if (e.stateMask == SWT.CTRL && e.keyCode == 119) {
					actionSend();
					e.doit = false;
				//esc
				} else if (e.keyCode == SWT.ESC) {
					actionToDoc();					
					e.doit = false;					
				}
			}

			public void keyReleased(KeyEvent e) {}
			
			
		});
	}
	
	
	/**
	 * 发送的action
	 *
	 */
	private void actionSend() {
		
		if (title.getText() == null || title.getText().length() == 0) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "文章发表", "标题不能为空");
			this.title.setFocus();
			return;
		}
		

		try {
			if (en == 0)
				BBSConAction.saveDocContent(board.getText(), Config.DOC_SIGN, "2", title.getText(), doc.getText());
			else if (en == 1)
				BBSConAction.saveDocContent(board.getText(), Config.DOC_SIGN, "1", title.getText().replaceFirst(" ", "%20"), doc.getText());
			actionToDoc();
		} catch (Exception e) {
			MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
			title.setFocus();
		}
	}
	
	
	/**
	 * 回文章列表
	 *
	 */
	private void actionToDoc() {
		//跳转回到用户操作列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			if (en == 0 || en == 1) {
				DocView d = (DocView)page.showView(DocView.ID);
				d.refreshData(-1, null, null, SendDocView.d, -1, null);
			}
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}

	public void refreshData(int en, String board, String title, int d) {
		if (en != -1) SendDocView.en = en;
		if (board != null) {
			this.board.setText(board);
		}
		if (title != null) {
			if (title.startsWith("★  "))
				this.title.setText("Re: " + title.substring(3));
			else if (title.startsWith("Re: "))
				this.title.setText(title);
			else
				this.title.setText("Re: " + title);
			this.title.setEditable(false);
		}
		
		SendDocView.d = d;
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		title.setFocus();
	}
}